<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class ProductCustomer extends Eloquent
{
	use SoftDeletingTrait;

	public $timestamps   = true;
	protected $dates     = ['deleted_at'];
	protected $table     = 'product_customer';
	protected $validator = null;

	public function saveData($data)
	{
		if(!$product_customer = $this->checkExists($data['product'], $data['customer'])){
			$product_customer = new ProductCustomer;
		}

		$product_customer->product  = $data['product'];
		$product_customer->customer = $data['customer'];
		$product_customer->price    = $data['price'];

		$product_customer->save();
		
		return $product_customer;
	}

	public function scopeProductCustomers($query, $customer){
		$resultNotIn = $query->select('product')->whereCustomer($customer)->get()->toArray();

		if(count($resultNotIn) > 0){
			$result = DB::table('product as p')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('p.id', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('p.deleted_at', '=', NULL)
						->whereNotIn('p.id', $resultNotIn)
						->orderBy('tb.order')
						->orderBy('p.net')
						->orderBy('p.name')
						->get();
		}else{
			$result = DB::table('product as p')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('p.id', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('p.deleted_at', '=', NULL)
						->orderBy('tb.order')
						->orderBy('p.net')
						->orderBy('p.name')
						->get();
		}

		return $result;
	}

	public function scopeSelectedProducts($query, $customer){
		$result = DB::table('product_customer as pc')
						->join('product as p', 'pc.product', '=', 'p.id')
						->join('taxonomy as tb', 'tb.id', '=', 'p.brand')
						->join('taxonomy as tt', 'tt.id', '=', 'p.type')
						->select('pc.id', 'pc.price', 'p.id as product', 'p.name as name', 'p.net', 'tb.taxonomy as brand', 'tt.taxonomy as type')
						->where('pc.deleted_at', '=', NULL)
						->where('p.deleted_at', '=', NULL)
						->where('customer', '=', $customer)
						->orderBy('tb.order')
						->orderBy('p.net')
						->orderBy('p.name')
						->get();

		return $result;
	}

	public function validator($data)
	{
		$rules = array(
				'price' => 
					array(
						'required',
						'numeric',
						'min:0',
					),
				);

		$this->validator = Validator::make($data, $rules);

		return $this->validator;
	}

	public function getValidatorMessage(){
		$messages = $this->validator->messages();
		
		$result   = array();

		foreach($messages->all() as $m){
			$result[]['message'] = $m;
		}

		return $result;
	}

	public function checkExists($product, $customer){
		$data = DB::table('product_customer')->where('product', '=', $product)->where('customer', '=', $customer)->first();

		$id = (isset($data->id))? $data->id : 0;

		$result = false;

		if($id != NULL && $id > 0){
			$result = ProductCustomer::find($id);
		}

		return $result;
	}
}